/**************************************************************************
 *
 * Copyright (C) 2007 University of California, Los Angeles.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 *************************************************************************/

package edu.ucla.cs.typecast.rmi.test;

import java.io.*;
import java.net.*;

import edu.ucla.cs.typecast.rmi.TypeCastClient;
import edu.ucla.cs.typecast.rmi.TypeCastServer;

import junit.framework.*;


class FooImpl implements Foo {
	protected String message = "(no message set)";

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		System.out.println("message changed from \"" + this.message + "\" to \"" + message + "\"");
		this.message = message;
	}
}

public class RmiTestCase extends TestCase {
	
	public void testGetAddress() throws UnknownHostException {
		final String ADDR = "239.255.10.10";
		assertEquals(ADDR, InetAddress.getByName(ADDR).getHostAddress());
	}
	
	public void testFunctionCall(String serverAddr, int serverPort, String clientAddr, int clientPort) throws IOException, UnknownHostException {
		// On the callee side, we create an object, 
		// set its message to "hello" and export this object.
		
		Foo remoteFoo = new FooImpl();
		remoteFoo.setMessage("hello");
		assertEquals("hello", remoteFoo.getMessage());
		
		TypeCastServer tpServer = new TypeCastServer(InetAddress.getByName(serverAddr), serverPort);
		tpServer.export(remoteFoo.getClass().getInterfaces(), remoteFoo);
		
		// On the caller side, we create a proxy for that object.
		
		TypeCastClient tpClient = new TypeCastClient(InetAddress.getByName(clientAddr), clientPort);
		Foo fooProxy = (Foo) tpClient.getTypeCastInvocationHandler(Foo.class);
		
		// When the caller gets setMessage() of that object, 
		// the message in the remote object should change to "world",
		// but the caller should get null as the return value.
		
		fooProxy.setMessage("world");
		try { 
			Thread.sleep(5000);
		} catch(Throwable t) {}
		assertEquals("world", remoteFoo.getMessage());
		assertEquals(null, fooProxy.getMessage());
		
		// Clean up
		
		tpServer.unexport(remoteFoo);
	}
	
	public void testMulticast() throws IOException, UnknownHostException {
		final String[] GROUP_ADDRS = {"239.255.10.10"};
		final int[] GROUP_PORTS = {5265};
		
		for (String addr : GROUP_ADDRS) {
			for (int port : GROUP_PORTS) {
				System.out.println("Mcast test, addr " + addr + ", port " + port);
				testFunctionCall(addr, port, addr, port);
			}
		}
	}

	public void testUnicast() throws IOException, UnknownHostException {
		final String CALLEE_ADDR = "127.0.0.1";
		final int CALLEE_PORT = 1234;
		
		testFunctionCall(CALLEE_ADDR, CALLEE_PORT, CALLEE_ADDR, CALLEE_PORT);
	}
	
	public static void main(String[] args) { 
		try {
			RmiTestCase test = new RmiTestCase(); 
			test.testMulticast(); 
			test.testUnicast(); 			
		} catch(Throwable t) { 
			t.printStackTrace(); 
		}
	}
}
